<?php
session_start();
function __autoload($name){
	include '../class/'.strtolower($name).'.class.php';
}
$art=new Article;
$entity=new Entity;
$deal=new dealComments;
$user=new User;

if($_POST['action']=='reply'){
	$uid=$_SESSION['id']?$_SESSION['id']:0;		//用户不登录，则uid赋值为0，因为不能为空
	$content=$entity->write($_POST['content']);
	$bool=$art->setComments($uid,$content,$_POST['aid']);
	die($bool);			//将bool值返回给ajax，不再往下执行代码
}else if($_POST['action']=='replyTo'){
	$uid=$_SESSION['id']?$_SESSION['id']:0;
	$content=$entity->write($_POST['content']);
	$bool=$art->setComments($uid,$content,$_POST['aid'],$_POST['floor']);
	die($bool);
}

$comm=$art->getComments($_POST['aid']);
$count=count($comm);

/**********/				//0.将一个回复分为四个部分：盒盖、回复content及之前，content之后到回复button，结束标签</div>
/*评论分类*/				//1.button由布尔值判定是否要添加，针对楼层相同时，第一次以后不添加button
/**********/				//2.两层循环：i、j，i为最外层，从0开始，j从1开始，检查i与i+j有无相同楼层，进而处理
							//3. tricky one
/**********/
/*评论列表*/
/**********/

/**********/
/**楼排序**/
/**********/
$rootPath=$art->path;
function floorPic($uid){
	global $art;
	global $user;
	if($uid){
		$picname=$user->getHeadPic($uid);
		$pic='images/users/'.$picname;			//头像------游客判定
		$pic=file_exists($pic)?$pic:'images/minicode.jpg';
	}else
		$pic='images/minicode.jpg';
	return $pic;
}
$output='';
$bool=true;
for($i=0;$i<$count;){
	$tmp=$i;
	$pic=floorPic($comm[$i]['repl_id']);
	$replier=$comm[$i]['replier']?$comm[$i]['replier']:'游客';		//用户名----游客判定
	$time=date('Y-m-d H:i:s',$comm[$i]['time']);
	$first=$deal->mid($pic,$replier,$comm[$i]['floor'],$time,$comm[$i]['content'],$_POST['aid'],true);
	$output.=$deal->head.$first[0];
	for($j=1;($i+$j)<$count;$j++){									// head out[0]/first[0] out[1]/first[1] footer
		if($comm[$i]['floor']==$comm[$i+$j]['floor']){
			$pic=floorPic($comm[$i+$j]['repl_id']);
			$replier=$comm[$i+$j]['replier']?$comm[$i+$j]['replier']:'游客';		//用户名-----游客判定

			$floor=$comm[$i+$j]['floor'];
			$time=date('Y-m-d H:i:s',$comm[$i+$j]['time']);
			$cont=$comm[$i+$j]['content'];
			$out=$deal->mid($pic,$replier,false,$time,$cont,$_POST['aid'],false);	// false 不要回复按钮
			$output.=$deal->head.$out[0].$out[1].$deal->footer;
		}else{
			$i+=$j;
			break;
		}

	}
	$output.=$first[1].$deal->footer;
	if($tmp==$i)
		break;
}
/********/
/*评论框*/
/********/
echo $output;
?>
<textarea id="reply" rows="8" cols="40" style="resize:none" placeholder="回复吧"></textarea>
<button id="reply_submit" class="btn btn-primary offset10" >确认回复</button>
<div id="reply_to_box" style="display:none;position:fixed;right:-100px;bottom:100px" class="indexBlock content">
	<div class="span2">
		<button id="reply_to_submit" class="btn btn-primary" style="margin:10px 0px"></button>
		<button id="reply_to_cancel" class="btn btn-primary" >取<br>消</button>
	</div>
	<textarea id="reply_to_content" class="span8" rows="8" cols="40" style='resize:none' placeholder="回复"></textarea>
</div>

